package com.longmao.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longmao.commonutils.JwtUtils;
import com.longmao.commonutils.R;
import com.longmao.commonutils.ordervo.CourseWebVoOrder;
import com.longmao.eduservice.client.OrderClient;
import com.longmao.eduservice.entity.EduCourse;
import com.longmao.eduservice.entity.vo.ChapterVo;
import com.longmao.eduservice.entity.vo.CourseEditVo;
import com.longmao.eduservice.entity.vo.CourseFrontVo;
import com.longmao.eduservice.service.EduChapterService;
import com.longmao.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author longmao
 * @date 2022/2/23 13:39
 * Content: 前台课程
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    /**
     * 分页查询课程的方法
     * @param page
     * @param limit
     * @param courseQuery
     * @return
     */
    @ApiOperation(value = "分页课程列表")
    @PostMapping("/pageCourseList/{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseFrontVo courseQuery){
        Page<EduCourse> pageParam = new Page<EduCourse>(page, limit);
        Map<String, Object> map = courseService.pageCourseList(pageParam, courseQuery);
        return  R.ok().data(map);
    }

    @GetMapping("/getCourseEditInfo/{courseId}")
    public R getCourseEditInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息
        CourseEditVo courseEditInfo = courseService.getCourseEditInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);
        //查询课程是否已经购买
        boolean isbuyCourse = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("courseEditInfo", courseEditInfo).data("chapterVideoList", chapterVideoList).data("isbuyCourse", isbuyCourse);
    }

    //根据课程id查询课程信息
    @PostMapping("/getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id) {
        CourseEditVo courseEditVo = courseService.getCourseEditInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseEditVo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}
